<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_products.php');
class ProductsControl
{
	var $products_m;
	function __construct()
	{
	
		$this->products_m = new ProductsModule();
	}
	/**
	* 得到页面属性列表
	*
	* @return  array
	*/
	function  get_all_attributes($id = 0,$webType = 1)
	{
		if(!empty($id))
		{
			$sql = 'SELECT name,id FROM product_attributes WHERE id = '.(int)$id.'  AND  is_delete = 0 AND web_type ='.(int)$webType;
		}else
		{
			$sql = 'SELECT name,id FROM product_attributes WHERE is_delete = 0 AND web_type = '.(int)$webType;;
		}
		
		return $this->products_m->get_date_info($sql);
	}
	/**
	* 得到bar
	*
	* @return  array
	*/
	function  get_all_bar($id = 0,$webType = 1)
	{
		if(!empty($id))
		{
			$sql = 'SELECT block_id ,block_name  FROM block  WHERE block_id  = '.(int)$id.' AND  block_delete = 0 AND web_type = '.(int)$webType;
		}else
		{
			$sql = 'SELECT block_id ,block_name  FROM block  WHERE block_delete  = 0 AND web_type = '.(int)$webType;
		}
	
		return $this->products_m->get_date_info($sql);
	}
	/**
	* 根据商品id得到商品信息
	* @param   int        $public_product_id     公共产品id
	* @param   int        $product_id             商品id
	* @return  array
	*/
	function  get_product_id($type,$public_product_id = 0,$product_id = 0, $webType = 1 )
	{
		if($type == 'ajax')
		{
			$sql = "SELECT id FROM won_product WHERE won_product_id = '$public_product_id' AND  won_product_delete = 0";
			
			$query = $GLOBALS['db']->query($sql);
			return $GLOBALS['db']->num_rows($query);
		}elseif ($type == 'get_name')
		{
			$sql = "SELECT  product.product_name   FROM won_product AS won_p  LEFT JOIN product AS product 
			ON won_p.won_product_id =  product.product_id   WHERE won_p.id = $product_id  AND  won_p.won_product_delete = 0";
		}
		elseif($type == 'edit')
		{
		
			$date_info = $attributes = $date_page = array();
			$sql = "SELECT * FROM won_product AS won_p  
					LEFT JOIN product AS product ON won_p.won_product_id = product.product_id WHERE won_p.id = '{$product_id}'";
			$date_info =  $this->products_m->get_date_info($sql);
			if(!empty($date_info[0]['won_attributes_id']))
			{
				$attributes = explode(',',$date_info[0]['won_attributes_id']);
			}
			if(!empty($attributes))
			{
				$str_attributes = '';
				foreach ($attributes as $key=>$value)
				{
					$sql2 = "SELECT page.page_id ,attr.name, page.product_id,page.article_id ,page.page_title ,page.page_content 
					FROM  product_page   AS page LEFT JOIN product_attributes  AS attr ON attr.id=  page.article_id
					WHERE page.product_id  = $product_id  AND page.article_id  = $value  AND page.page_delete = 0 ";
					
					$row = $this->products_m->get_date_info($sql2);
					if(!empty($row[0]))
					{
						$str_attributes.=$value.',';
						$date_page[$value] = $row[0];
					}
				}
				$date_info[0]['won_attributes_id'] = substr($str_attributes, 0, strlen($str_attributes)- 1 );
				$date_info[0]['data_page'] = $date_page;
			}
			
			//下面代码是根据id取名称
			if(!empty($date_info[0]['won_attributes_id']))
			{
				$attributes_str = '';
				$attributes = explode(',',$date_info[0]['won_attributes_id']);
				for($i = 0 ;$i<count($attributes);$i++)
				{
				$att_date =  $this->get_all_attributes($attributes[$i],$webType);
				$attributes_str = $attributes_str.','.$att_date[0]['name'];
				}
				unset($attributes);unset($att_date);
			}
		
			if(!empty($date_info[0]['won_overview_bar']))
			{
				$bar_str = '';
				$bars = explode(',',$date_info[0]['won_overview_bar']);
				for($j = 0 ;$j<count($bars);$j++)
				{
				$bar_date =  $this->get_all_bar($bars[$j],$webType);
				$bar_str = $bar_str.','.$bar_date[0]['block_name'];
				}
				unset($bars);unset($bar_date);
			}
			if(!empty($date_info[0]['recommended_product']))
			{
				$recommended_str = '';
				$recommended_products = explode(',',$date_info[0]['recommended_product']);
				for($k = 0 ;$k<count($recommended_products);$k++)
				{
					$recommended_date =  $this->get_product_id('get_name',0,$recommended_products[$k]);
					
					$recommended_str = $recommended_str.','.$recommended_date[0]['product_name'];
				}
				unset($recommended_products);unset($recommended_date);
			}
			$high_version =  $this->get_product_id('get_name',0,$date_info[0]['high_product']);
			
			$date_info[0]['attributes_name'] = substr($attributes_str,1);
			$date_info[0]['bar_name'] = substr($bar_str,1);
			$date_info[0]['recommended_name'] = substr($recommended_str,1);
			$date_info[0]['high_product_name'] = $high_version[0]['product_name'];
			//上面代码是根据id取名称
			return $date_info[0];
		}
		return $this->products_m->get_date_info($sql);
	}
	/**
	* 得到子分类字符串
	*
	* @param   string      $list_id       以逗号隔开的文章id字符串
	* @return  int
	*/
	function get_sub_category($cat_id)
	{
		$sql = "SELECT cat_id   FROM  category  WHERE parent_path  LIKE '%".$cat_id."%' ";
		$str = $this->products_m->get_sub_category($sql);
		return $str.$cat_id;
	}
	/**
	* 得到数据提交条件
	*
	* @param   int        $cat_id              分类id
	* @param   int        $won_product_delete  是否删除
	* @param   int	    $product_id       	产品ID
	* @param   string     $product_name        产品名称
	* @param   string     $tag                 标签 
	* @param   string     $is_emphasis         是否重要
	* @return  string
	*/
	function handle_post($cat_id,$won_product_delete,$product_id,$product_name,$tag,$is_emphasis)
	{ 
		$where_str = '';
		if(!empty($cat_id))
		{
			$sub_str = $this->get_sub_category((int)$cat_id);
			$where_str.=' AND cat.cat_id IN ('.$sub_str.')';
		}
		if(!empty($tag))
		{
		
			$where_str.=" AND won_p.tag LIKE '%".$tag."%' ";
		}
		if(!empty($is_emphasis))
		{
			$is_emphasis = (int)$is_emphasis == 2?0:(int)$is_emphasis ;
			$where_str.=' AND won_p.is_emphasis = '.$is_emphasis;
		}
		if(!empty($product_id))
		{
			$where_str.=' AND ( product.product_id = '.(int)$product_id. ' OR won_p.id ='.(int)$product_id.') ';
		}
		if(!empty($product_name))
		{
			$product_name = trim($product_name);
			$where_str.=" AND product.product_name LIKE '%".$product_name."%' ";
		}
		return  $where_str;
	}
	/**
	* 得到数据提交条件返回url参数
	*
	* @param   int        $cat_id              分类id
	* @param   int        $won_product_state   是否发布
	* @param   int        $won_product_delete  是否删除
	* @param   int	    $product_id			   产品id
	* @param   string     $product_name        产品名称
	* @param   string     $tag                 标签 
	* @param   string     $is_emphasis         是否重要
	* @return  string
	*/
	function get_url_par($cat_id,$won_product_delete,$product_id,$product_name,$tag,$is_emphasis)
	{ 
		$url_str = '&url_page=1';
		if(!empty($cat_id))
		{
			$url_str.='&cat_id='.(int)$cat_id;
		}
		if(!empty($tag))
		{
			$url_str.='&tag='.$tag;
		}
		if(!empty($is_emphasis))
		{
			$url_str.='&is_emphasis='.(int)$is_emphasis;
		}
		if(!empty($product_id))
		{
			$url_str.='&pr_id='.(int)$product_id;
		}
		if(!empty($product_name))
		{
			$product_name = trim($product_name);
			$url_str.='&pr_name='.urlencode(str_replace('\\','',$product_name));
		}
		return  $url_str;
	}
	/**
	* ajax修改标签
	*
	* @param   int        $tag_id          标签id
	* @param   string     $change_value    要修改的值
	* @param   string     $field           数据字段
	* @param   string     $type           为one_update代表为单条修改 more_update为多条修改
	* @return  int
	*/
	function  change_info($products_id,$change_value,$field,$type = '')
	{
		if($type == 'one_update')
		{
			$sql = "UPDATE won_product SET $field ='$change_value',modify_status = 1 WHERE id  = $products_id ";
		}elseif($type == 'more_update')
		{
			$sql = "UPDATE  won_product  SET won_product_delete = 1 WHERE $field IN ($change_value) ";
		}
		
		return $this->products_m->change_info($sql);
	}
	/**
	* 得到分页相关信息
	*
	* @param   int        $page               	 页码
	* @param   int        $type               	 为list代表没有条件查询 为list_s代表有条件查询
	* @param   int        $cat_id              	分类id
	* @param   string     $webType              网站类型
	* @param   int        $won_product_delete 	 是否删除
	* @param   int	    $product_id			            产品名称
	* @param   string     $product_name        	 产品名称
	* @param   string     $tag  				标签
	* @param   int        $is_emphasis        	是否重要
	* @param	 string 	$sortby				排序字段
	* @param	 string		$sort				排序类型

	* @return  array
	*/
	function get_url_info($page,$type,$pagesize=10, $webType = 1,$cat_id = 0,$won_product_delete =0,$product_id = 0,$product_name = '',
	$tag = '',$is_emphasis = 0, $sortby = '', $sort = '')
	{
		$url_str = '';
		if($type == 'list')
		{
			$sql_p = 'SELECT COUNT(*) AS total FROM won_product WHERE won_product_delete = 0 AND web_type = '.$webType;
		}elseif($type == 'list_s')
		{
			$url_str = $this->get_url_par($cat_id,$won_product_delete,$product_id,$product_name,$tag,$is_emphasis);
		
			$where_str = $this->handle_post($cat_id,$won_product_delete,$product_id,$product_name,$tag,$is_emphasis);
		
			$sql_p = 'SELECT COUNT(*) AS total FROM won_product AS won_p LEFT JOIN product AS 
			product ON won_p.won_product_id = product.product_id LEFT JOIN category AS cat ON 
			won_p.cat_id = cat.cat_id  WHERE won_p.won_product_delete = 0 AND web_type = '.$webType.$where_str;
		}
		
		$page_info = get_page_info($sql_p,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表或者其它的tag信息
	*
	* @param   string     $type        		        类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      		       从那行开始
	* @param   int        $pagesize   		        取多少条记录
	* @param   int        $cat_id              分类id
	* @param   string     $webType              网站类型
	* @param   int        $won_product_state   是否发布
	* @param   int        $won_product_delete  是否删除
	* @param   int	      $product_id          产品id
	* @param   string     $product_name        产品名称
	* @param   string     $tag                 标签 
	* @param   string     $is_emphasis         是否重要
	* @param   string 	  $sortby			         排序字段
	* @param   string	  $sort			                   排序类型
	* @return  array
	*/
	function  get_products_info($type,$start = 0,$pagesize = 10, $webType = 0,$cat_id = 0,$won_product_delete =0,
	$product_id = 0,$product_name = '',$tag = '',$is_emphasis = 0,$sortby = '', $sort = '')
	{
	
		$order_by = $pagesize == 160?' product.product_name ASC ':' sort ASC,won_p.won_product_add_time DESC';
		if( !empty($sortby) ) $order_by = $sortby .' '. $sort; 
		$sqlPrefix = 'SELECT won_p.id,won_p.tag ,won_p.sort,product.product_id,product.product_name ,cat.cat_title,product.product_cbsid,won_p.is_emphasis,
		won_p.won_product_add_user,won_p.won_product_add_time ,won_p.won_product_update_user , won_p.won_product_update_time 
		FROM won_product AS won_p LEFT JOIN product AS product ON won_p.won_product_id =  product.product_id 
		LEFT JOIN category AS cat ON won_p.cat_id = cat.cat_id WHERE won_p.won_product_delete = 0 AND web_type = '.$webType;
		if($type == 'list')
		{
			$sql = $sqlPrefix.' ORDER BY '.$order_by.'  LIMIT '.$start.','.$pagesize ;
		
		}elseif($type == 'list_s')
		{ 
			$where_str = $this->handle_post($cat_id,$won_product_delete,$product_id,$product_name,$tag,$is_emphasis);
			$sql = $sqlPrefix.$where_str.' ORDER BY '.$order_by.'  LIMIT '.$start.','.$pagesize ;
			
		
		}
		
		return $this->products_m->get_date_info($sql,'list');
	}
	/**
	* 增加或修改产品
	* @param   array        $post_arr     提交数组
	* @return  int
	*/
	function  change_won_product($post_arr)
	{
		if($post_arr['act'] == 'add')
		{
			$sql = "INSERT INTO won_product (cat_id ,won_product_id ,won_attributes_id ,won_product_add_time ,
					won_product_add_user , won_product_guide_bar,won_techspec_bar,won_overview_bar,tips,tag,
					is_emphasis,top_image,recommended_product, is_high_version,high_product,advantages_content,awards_image,
					won_product_update_user,won_product_update_time,giveaway, getcodeurl,ad_guide,overview_desc,overview_img,
					overview_append,screenshots,web_type,is_review ) 
					VALUES ('{$post_arr['parent_id']}','{$post_arr['products_id']}','{$post_arr['w_attributes_id']}', 
					'{$_SERVER['REQUEST_TIME']}','{$_SESSION['AName']}', '{$post_arr['guide_bar']}',
					'{$post_arr['techspec_bar']}','{$post_arr['overview_bar']}','{$post_arr['tips']}','{$post_arr['tag']}',
					'{$post_arr['is_emphasis']}','{$post_arr['top_image']}','{$post_arr['recommended_product']}', 
					'{$post_arr['is_high_version']}','{$post_arr['high_product']}','{$post_arr['advantages_content']}',
					'{$post_arr['awards_image']}','{$_SESSION['AName']}', '{$_SERVER['REQUEST_TIME']}','{$post_arr['giveaway']}',
					'{$post_arr['getcodeurl']}','{$post_arr['ad_guide']}','{$post_arr['overview_desc']}',
					'{$post_arr['overview_img']}','{$post_arr['overview_append']}','{$post_arr['screenshots']}','{$post_arr['webType']}',
					'{$post_arr['is_review']}') "; 
			
		}elseif($post_arr['act'] == 'edit')
		{
			$sql = "UPDATE won_product SET cat_id = '{$post_arr['parent_id']}',won_attributes_id = '{$post_arr['w_attributes_id']}', 
					won_product_update_time = '{$_SERVER['REQUEST_TIME']}' ,won_product_update_user = '{$_SESSION['AName']}' ,
					won_product_guide_bar = '{$post_arr['guide_bar']}', high_product = '{$post_arr['high_product']}' ,
					advantages_content = '{$post_arr['advantages_content']}',won_techspec_bar = '{$post_arr['techspec_bar']}',
					won_overview_bar = '{$post_arr['overview_bar']}',tips = '{$post_arr['tips']}' ,
					is_high_version = '{$post_arr['is_high_version']}' ,tag = '{$post_arr['tag']}' ,
					is_emphasis = '{$post_arr['is_emphasis']}' ,top_image = '{$post_arr['top_image']}',
					recommended_product = '{$post_arr['recommended_product']}',awards_image = '{$post_arr['awards_image']}',
					giveaway = '{$post_arr['giveaway']}',getcodeurl = '{$post_arr['getcodeurl']}', is_review = '{$post_arr['is_review']}',
					ad_guide = '{$post_arr['ad_guide']}', overview_desc = '{$post_arr['overview_desc']}',
					overview_img = '{$post_arr['overview_img']}',overview_append = '{$post_arr['overview_append']}',screenshots = '{$post_arr['screenshots']}',
					modify_status = 1 
					WHERE id = '{$post_arr['products_id']}' AND won_product_delete = 0";
			return $this->products_m->change_info($sql);
		}

		return $this->products_m->get_insert_id($sql);		
	}

	/**
	* 操作
	*
	* @param   string        $type                   类型
	* @param   int           $products_id            产品id
	* @param   int           article_id              产品属性id（外键）
	* @param   string        $page_bar               关联bar块
	* @param   string        $page_title             页面标题
	* @param   string        $page_content           页面内容信息 
	* @return  string
	*/
	function  change_product_page($type,$products_id,$article_id ,$page_title = '' ,$page_content= '')
	{
		if($type == 'update')
		{
			$sql = "UPDATE  product_page  SET  page_title = '$page_title',page_content = '$page_content',
			page_update_time = '".$_SERVER['REQUEST_TIME']."' ,page_update_user = '".$_SESSION['AName']."'
			,modify_status = 1   WHERE  product_id  = '$products_id' AND article_id = '$article_id' AND page_delete  = 0 ";
		
		}elseif($type == 'add')
		{
			$sql1 = "SELECT page_id  FROM product_page  WHERE  product_id  = '$products_id' AND article_id = '$article_id' AND page_delete  = 1 ";
			$row = $this->products_m->get_date_info($sql1);
			if(!empty($row[0]['page_id']))
			{
				$sql = "UPDATE  product_page  SET  page_title = '$page_title',page_content = '$page_content',
				page_update_time = '".$_SERVER['REQUEST_TIME']."' ,page_update_user = '".$_SESSION['AName']."' 
				,modify_status = 1,page_delete = 0  WHERE  product_id  = '$products_id' AND article_id = '$article_id' ";
			}else
			{
				$sql ="INSERT INTO product_page  (product_id ,article_id  ,page_title  ,page_content    
				,page_add_user  ,page_add_time ,page_update_time, page_update_user) VALUES ('$products_id',
				'$article_id','$page_title','$page_content','".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."',
				'".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."') ";
			}
			
		}
		elseif($type == 'delete')
		{
			$sql ="UPDATE  product_page  SET  page_delete = 1 ,page_update_time = '".$_SERVER['REQUEST_TIME']."' ,page_update_user = '".$_SESSION['AName']."'  WHERE      
			product_id  = '$products_id' AND article_id = '$article_id' AND page_delete  = 0 ";
		
		}
		return $this->products_m->change_info($sql);
	}
	
	


}
?>